/**
 *  Copyright Matt Silbernagel 2010
 *  
 *	This file is part of MobilePrinting.
 *
 *	MobilePrinting is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MobilePrinting is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MobilePrinting.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.uc.labs.MobilePrinting.PrintUtils;

import static edu.uc.labs.MobilePrinting.Configurator.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;


/**
 * An XML Parser for Printer Configurations
 * 
 * @author Matthew Silbernagel
 */
public class PrinterParser {

	/**
	 * Constructs a parser that can parse Printer Configurations
	 */
	public PrinterParser() throws ParserConfigurationException {
		DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
		builder = dbfactory.newDocumentBuilder();
		XPathFactory xpfactory = XPathFactory.newInstance();
		path = xpfactory.newXPath();

	}

	/**
	 * Parses an XML file containing a printer list
	 * 
	 * @param fileName
	 *            the name of the file
	 * @return an array list containing all printers in the XML file
	 */
	public ArrayList<Printer> parse(String fileName) throws SAXException,
			IOException, XPathExpressionException {
		File f = new File(fileName);
		if (!f.exists()) {
			LOGGER.debug("PrinterParser:  The file to parse, " + fileName
					+ ", doesn't exist...");
			try {
				PrinterWriter writer = new PrinterWriter();
				writer.writeBlank();
				LOGGER.debug("Created the xml file");
			} catch (ParserConfigurationException e) {
				LOGGER.error("PrinterParser.java: Unable to write the printers.xml file: " + e.toString());
			}
		}

		LOGGER.debug("now Parsing " + f.getAbsolutePath());
		Document doc = builder.parse(f);

		ArrayList<Printer> printers = new ArrayList<Printer>();
		int printerCount = Integer.parseInt(path.evaluate(
				"count(/CONFIGURATIONS/PRINTERS/PRINTER)", doc));
		for (int i = 1; i <= printerCount; i++) {
			String name = path.evaluate("/CONFIGURATIONS/PRINTERS/PRINTER[" + i
					+ "]/NAME", doc);
			String server = path.evaluate("/CONFIGURATIONS/PRINTERS/PRINTER["
					+ i + "]/SERVER", doc);
			String queue = path.evaluate("/CONFIGURATIONS/PRINTERS/PRINTER["
					+ i + "]/QUEUE", doc);
			String ppd = path.evaluate("/CONFIGURATIONS/PRINTERS/PRINTER[" + i
					+ "]/DRIVER", doc);
			String zone = path.evaluate("/CONFIGURATIONS/PRINTERS/PRINTER[" + i
					+ "]/ZONE", doc);
			String location = path.evaluate("/CONFIGURATIONS/PRINTERS/PRINTER["
					+ i + "]/LOCATION", doc);
			String ppdURL = PPD + DELIM + zone + DELIM + ppd;
			
			Printer p = new Printer(name, queue, server, ppdURL, location);
			p.setZone(zone);
			p.setPPDFileName(ppd);
			p.setDriver(WINDRIVERUNC);
			printers.add(p);
		}
		if (!printers.isEmpty()) {
			setZone(printers.get(0).getZone());
		}
		return printers;
	}

	private DocumentBuilder builder;
	private XPath path;
}
